package com.github.prontera.job;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.github.prontera.common.filter.limit.LimiterStatsService;
import com.github.prontera.common.filter.limit.LimiterStatsService.LimiterCallback;
import com.github.prontera.common.filter.limit.LimiterStatsService.LimiterContext;
import com.github.prontera.domain.ScheduleJob;

/**
 * 计划任务执行处 无状态
 */
public class QuartzJobFactory implements Job {

	public final Logger log = Logger.getLogger(this.getClass());

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
		String key = scheduleJob.getJobGroup() + scheduleJob.getJobName();
		LimiterStatsService.getInstance().execute(key, 1, 1, new LimiterCallback() {

			@Override
			public void noexecute(LimiterContext limiterContext) {
			}

			@Override
			public void execute(LimiterContext limiterContext) {
				TaskUtils.invokMethod(scheduleJob);
			}
		});
	}
}